﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using CP.Entities;
using CP.DBService;
using CP.Utility;
using System.Data;

namespace CP.Repositories.GPM
{
    public class UserFuncRepository
    {
        DBHelper db = DBHelper.GPMCreateInstance();

        public IList<Menu> GetAllMenus()
        {
            return db.ExecuteObject<Menu>("select * from Menu order by Menu_id");
        }

        public DataTable GetResourceInfo(string type, string resourcename)
        {
            string sql = "";
            if (type == "0")
            {
                sql = string.Format(@"SELECT t1.menu_id resource_id ,
                            t1.menu_name resource_name, t2.menu_name resource_info,t1.webpart_id,0 menu_id
                              FROM (SELECT *
                                      FROM menu
                                     WHERE menu_name LIKE '%{0}%') t1,
                                   menu t2
                             WHERE t1.parent_id = t2.menu_id(+)
                             order by t1.parent_id,t1.sort_order", resourcename);
                return db.ExecuteDataTable(sql);
            }
            else
            {
                sql = string.Format(@"SELECT t1.button_id resource_id,
                                t1.button_name resource_name,
                                 t2.menu_name resource_info, t2.webpart_id, t2.menu_id 
                                    FROM button_list t1, menu t2
                           WHERE t1.webpart_id = t2.webpart_id and
                                 t1.button_name LIKE '%{0}%'
                        ORDER BY t2.parent_id, t2.sort_order", resourcename);
                return db.ExecuteDataTable(sql);
            }
            
        }

    }
}
